package com.sservice.banfu.controller;

import com.sservice.banfu.e.Tpl;
import com.sservice.banfu.service.TplService;
import com.sservice.base.entity.dto.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Condition;

import java.util.List;

/**
 * Created by byte_su@163.com on 2018/02/05.
 */
@Controller
@RequestMapping("/tpl/")
public class TplController {

    @Autowired
    TplService tplService;

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(Tpl tpl) {
        if (new Integer(0).equals(tpl.getId())) { // 首页
            Condition condition = new Condition(Tpl.class);
            condition.createCriteria().andEqualTo("plain").equals("HOME");
            List<Tpl> list = tplService.findByCondition(condition);
            if (list.size() > 0) {
                Tpl tpl2 = list.get(0);
                tpl2.setRemark(tpl2.getContent() != null ? new String(tpl2.getContent()) : "");

                return new BaseResult(true, tpl2);
            } else {
                return new BaseResult(false, "未设置首页！", 403);
            }
        }
        Tpl tpl1 = tplService.findById(tpl.getId());
        tpl1.setRemark(tpl1.getContent() != null ? new String(tpl1.getContent()) : "");
        return new BaseResult(true, tpl1);
    }

}
